package keye.document_manage.util;

import java.util.List;

import javax.swing.JTree;

import keye.document_manage.domain.ClassEntityRelation;
import keye.document_manage.domain.ClassEntityRelationWithArchive;
import keye.document_manage.service.IArchivenoFormatService;
import keye.document_manage.service.IClassEntityRelationService;
import keye.document_manage.gui.CheckNode;
import keye.document_manage.gui.CheckRenderer;;

public class TreeDemo {

	/**
	 * 
	 * @param classEntityRelationService
	 * @param archivenoFormatService
	 * @return
	 */
	public static JTree createDefaultTree(IClassEntityRelationService classEntityRelationService, IArchivenoFormatService archivenoFormatService){
		
		return getTree(classEntityRelationService, archivenoFormatService, false);
	}
	
	/**
	 * 带复选框的
	 * @param classEntityRelationService
	 * @param archivenoFormatService
	 * @return
	 */
	public static JTree createCheckTree(IClassEntityRelationService classEntityRelationService,IArchivenoFormatService archivenoFormatService){

		return getTree(classEntityRelationService, archivenoFormatService, true);
	}
	
	private static JTree getTree(IClassEntityRelationService classEntityRelationService,IArchivenoFormatService archivenoFormatService,boolean isCheck){
		List<ClassEntityRelation> list = classEntityRelationService.getTreeRoot();
		CheckNode root = new CheckNode();
		for( ClassEntityRelation cer : list){
			CheckNode tNode = new CheckNode(new ClassEntityRelationWithArchive(cer, null));
			root.add(tNode);
			createTree(tNode, cer.getId(), null, classEntityRelationService, archivenoFormatService);
		}
		JTree tree = new JTree(root);
		if(isCheck)
			tree.setCellRenderer(new CheckRenderer());
		tree.setRowHeight(20);
		tree.setRootVisible(false);//隐藏root
		tree.setShowsRootHandles(true);//显示句柄
		return tree;
	}

	/**
	 * 构建门类树时加载档号格式
	 * @param node
	 * @param pid
	 * @param classEntityRelationService
	 * @param archivenoFormatService
	 */
	private static void createTree(CheckNode node, String pid, String[][] pFormat,IClassEntityRelationService classEntityRelationService, IArchivenoFormatService archivenoFormatService){
		List<ClassEntityRelation> list = classEntityRelationService.getTreeNode(pid);
		for( ClassEntityRelation cer : list){
			String[][] cFormat = pFormat != null ? pFormat : archivenoFormatService.getSegmentRule(cer.getId());
			ClassEntityRelationWithArchive archive = new ClassEntityRelationWithArchive(cer, cFormat);
			CheckNode tNode = new CheckNode(archive);
			node.add(tNode);
			createTree(tNode, cer.getId(), cFormat, classEntityRelationService, archivenoFormatService);
		}
	}
	
//	public static JTree getDefaultTree(IClassEntityRelationService classEntityRelationService){
//	JTree tree = new JTree();
//	tree.setModel(new DefaultTreeModel(createTree(classEntityRelationService), false));
//	tree.setRootVisible(false);
//	addListener(tree);
//	return tree;
//	}

//	public static JTree creatCheckTree(IClassEntityRelationService classEntityRelationService){
//
//	List<ClassEntityRelation> list = classEntityRelationService.getTreeRoot();
//	CheckNode root = new CheckNode();
//	for( ClassEntityRelation cer : list){
//		CheckNode tNode = new CheckNode(cer);
//		root.add(tNode);
//		createTree(tNode, cer.getId(), classEntityRelationService);
//	}
//	JTree tree = new JTree(root);
//	tree.setCellRenderer(new CheckRenderer());
//		tree.getSelectionModel().setSelectionMode(
//				TreeSelectionModel.SINGLE_TREE_SELECTION);
//		return tree;
//	}
	
//	public static DefaultMutableTreeNode createTree(IClassEntityRelationService classEntityRelationService){
//		List<ClassEntityRelation> list = classEntityRelationService.getTreeRoot();
//		DefaultMutableTreeNode root = new DefaultMutableTreeNode();
//		for( ClassEntityRelation cer : list){
//			DefaultMutableTreeNode tNode = new DefaultMutableTreeNode(cer);
//			root.add(tNode);
//			createTree(tNode, cer.getId(), classEntityRelationService);
//		}
//		return root;
//	}

//	private static void createTree(DefaultMutableTreeNode node, String id,IClassEntityRelationService classEntityRelationService){
//		List<ClassEntityRelation> list = classEntityRelationService.getTreeNode(id);
//		for( ClassEntityRelation cer : list){
//			DefaultMutableTreeNode tNode = new DefaultMutableTreeNode(cer);
//			node.add(tNode);
//			createTree(tNode, cer.getId(), classEntityRelationService);
//		}
//	}


//	private static void createTree(CheckNode node, String id,IClassEntityRelationService classEntityRelationService){
//		List<ClassEntityRelation> list = classEntityRelationService.getTreeNode(id);
//		for( ClassEntityRelation cer : list){
//			CheckNode tNode = new CheckNode(cer);
//			node.add(tNode);
//			createTree(tNode, cer.getId(), classEntityRelationService);
//		}
//	}

	
}